import React from 'react';
import Table from '../../../components/table';
import { listByOperator as queryLaunch } from '../../../services/deviceLaunchArea';
import { subList as queryOperator } from '../../../services/operator';
import { agentChild as queryAgent } from '../../../services/agent';

const columnObj = {
	1: [
		{
			title: '投放点名称',
			dataIndex: 'name',
			key: 'name'
		},
		{
			title: '地点',
			dataIndex: 'region',
			key: 'region',
		},
		{
			title: '供应商',
			dataIndex: 'operatorName',
			key: 'operatorName'
		}
	],
	2: [
		{
			title: '供应商名称',
			dataIndex: 'name',
			key: 'name'
		},
		{
			title: '所属省市',
			dataIndex: 'area',
			key: 'area',
			render: (area, d) => `${d.province}/${d.city}/${area || ''}`
		}
	]
};

const queryFunObj = {
	1: queryLaunch,
	2: queryOperator,
	3: queryAgent
};

class MyTable extends Table {
	constructor(props, context) {
		super(props, context);
		this.columns = columnObj[props.target] || [];
	}

	componentWillReceiveProps(nextProps) {
		if (nextProps.target !== this.props.target) {
			this.setState({
				dataSource: [],
				current: 1,
				size: 10,
				total: 0
			}, () => this.query());
			this.columns = columnObj[nextProps.target] || [];
		}
	}

	queryData(data) {
		// 调用请求
		const { target } = this.props;
		if (!target) return;
		return queryFunObj[target](data);
	}

	merge() {
		const { current, size } = this.state;
		const { queryData } = this.props;
		return { current, size, query: { ...queryData } };
	}
}

export default MyTable;
